package com.baizhi.controller;


import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import com.baizhi.entity.BzAdmin;
import com.baizhi.service.IBzAdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lby
 * @since 2020-09-24
 */
@RestController
@RequestMapping("/admin")
public class BzAdminController {
    @Autowired
    private IBzAdminService iBzAdminService;

    @GetMapping("/admins")
    public @ResponseBody Map showPageAdmins(@RequestParam(value = "page",defaultValue = "1") Integer page, @RequestParam(value = "limit",defaultValue = "5") Integer limit){
        Map map1 = iBzAdminService.showPageAdmin(page, limit);
        Object count = map1.get("count");
        System.out.println("count = " + count);
        Object admins = map1.get("admins");
        System.out.println("admins = " + admins);
        Map map = new HashMap();
        map.put("code",0);
        map.put("count",map1.get("count"));
        map.put("data",map1.get("admins"));
        return map;
    }
   /* @GetMapping("loginController")
    public Map loginController(String username, String password){
        HashMap map = new HashMap();
        BzAdmin bzAdmin = iBzAdminService.login(username, password);
        if(bzAdmin==null){
            map.put("error","失败");
        }else{map.put("success","成功");}
        return map;
    }*/
  /* @PostMapping("/login/{code}")
   public Map login2(@RequestBody BzAdmin bzAdmin, @PathVariable("code") String code , HttpSession session){
       String realCode = (String)session.getAttribute("realCode");
       Map map = new HashMap();
       if(!code.equalsIgnoreCase(realCode)){
           map.put("msg","验证码有误");
           map.put("status","err");
           return map;
       }
       BzAdmin admin = iBzAdminService.login(bzAdmin.getUsername(),bzAdmin.getPassword());
       if(admin == null){
           map.put("msg","用户名或密码有误");
           map.put("status","err");
           return map;
       }
       map.put("status","success");
       System.out.println(map.toString());
       return map;
   }*/

    @RequestMapping("/validateCode")
    public void validateCode(HttpServletRequest req, HttpServletResponse resp)throws Exception {
        //生成线条影响的验证码
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(200, 100);
        //获取线条生成的真正验证码
        String code = captcha.getCode();
        HttpSession session = req.getSession();
        session.setAttribute("realCode", code);
        //将验证码响应回浏览器
        OutputStream out = resp.getOutputStream();
        captcha.write(out);
    }

    @GetMapping("getCaptcha")
    public void getCaptcha(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        HttpSession session = req.getSession();
        //生成验证码
        LineCaptcha captcha = CaptchaUtil.createLineCaptcha(80, 40);
        String code = captcha.getCode();
        session.setAttribute("realCode", code);
        //将验证码响应回浏览器
        OutputStream out = resp.getOutputStream();
        captcha.write(out);
    }
}
